#import "ALElement+DisplayAdditions.h"



#pragma mark Constants
static const CGFloat ALElementPrimaryLabelFontSize = 18.0f;
static const CGFloat ALElementSecondaryLabelFontSize = 14.0f;
static const CGFloat ALElementValueFontSize = 13.0f;
static NSString* const ALElementStarString = @"★";
static NSString* const ALElementHalfStarString = @"½";
static NSString* const ALElementEmptyValueString = @"[No value]";


#pragma mark -
#pragma mark Class Globals
static NSDictionary *ALElementPrimaryLabelAttributes;
static NSDictionary *ALElementSecondaryLabelAttributes;
static NSDictionary *ALElementValueAttributes;
static NSDateFormatter *ALElementValueDateFormatter;
static BOOL ALElementAttributesInitialized;


#pragma mark -
@interface ALElement (DisplayAdditionsPrivateMethods)
-(void)initializeClassGlobals;
-(NSString *)elementValueString;
@end


#pragma mark -
@implementation ALElement (DisplayAdditions)

-(void)initializeClassGlobals {
	ALElementPrimaryLabelAttributes = [NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:ALElementPrimaryLabelFontSize] forKey:NSFontAttributeName];
	ALElementSecondaryLabelAttributes = [NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:ALElementSecondaryLabelFontSize] forKey:NSFontAttributeName];
	ALElementValueAttributes = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:ALElementValueFontSize] forKey:NSFontAttributeName];
	ALElementValueDateFormatter = [[NSDateFormatter alloc] init];
	[ALElementValueDateFormatter setDoesRelativeDateFormatting:YES];
	ALElementAttributesInitialized = YES;
}

-(NSAttributedString *)attributedStringForDisplay {
	if (!ALElementAttributesInitialized) 
		[self initializeClassGlobals];
	if ([[self elementType] integerValue] == ALElementTypeAttachment)
		return nil;
	NSInteger line = [[self lineNumber] integerValue];
	NSDictionary *labelAttributes = (line == 0) ? ALElementPrimaryLabelAttributes : ALElementSecondaryLabelAttributes;
	NSString *elementLabelString = [NSString stringWithFormat:@"%@\n", [self label]];
	NSMutableAttributedString *elementAttributedString = [[NSMutableAttributedString alloc] initWithString:elementLabelString attributes:labelAttributes];
	NSAttributedString *elementValueString = [[NSAttributedString alloc] initWithString:[self elementValueString] attributes:ALElementValueAttributes];
	[elementAttributedString appendAttributedString:elementValueString];
	return elementAttributedString;
}

-(NSString *)elementValueString {
	ALElementType type = (ALElementType)[[self elementType] integerValue];
	NSString *elementString = @"";
	NSMutableString *ratingString = [NSMutableString string];
	switch (type) {
		case ALElementTypeText:
			elementString = [self textValue];
			break;
		case ALElementTypeDate:
			elementString = [ALElementValueDateFormatter stringFromDate:[self dateValue]];
			break;
		case ALElementTypeRating:
			for (CGFloat ratingIndex = 0; ratingIndex < [[self ratingValue] floatValue]; ratingIndex += 1.0f) {
				if ([[self ratingValue] floatValue] - ratingIndex > 0.5f)
					[ratingString appendString:ALElementStarString];
				else 
					[ratingString appendString:ALElementHalfStarString];
			}
			elementString = ratingString;
			break;
		default:
			break;
	}
	if (!elementString || [elementString length] == 0)
		elementString = ALElementEmptyValueString;
	return elementString;
}

@end
